package com.itheima.controller;

import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.UnitValue;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * <h1>pdf页眉页脚事件</h1>
 *
 * @author <a href="11n_yd2h5e1jx8@dingtalk.com">叶世铭</a>
 * @since 2024/1/9
 */
@Slf4j
public class PdfHeaderFooterEvent implements IEventHandler {

    /**
     * 事件处理
     */
    @Override
    public void handleEvent(Event event) {
        final PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
        final PdfDocument pdfDoc = docEvent.getDocument();
        final Document doc = new Document(pdfDoc);
        final PdfPage page = docEvent.getPage();
        final Rectangle pageSize = page.getPageSize();

        try {
            this.addPageHeader(doc);
        } catch (Exception e) {
            log.error("添加页眉出错", e);
        }
        try{
            this.addPageFooter(doc, pdfDoc, page);
        }catch(Exception e){
            log.error("添加页脚出错", e);
        }
    }

    /**
     * 页眉
     */
    private void addPageHeader(Document document) throws IOException {
        //创建字体
        final String logo = "src/main/resources/u1457.png";
        PdfFont font = PdfFontFactory.createFont("src/main/resources/AlimamaShuHeiTi-Bold.ttf", PdfEncodings.IDENTITY_H);

        Image image = new Image(ImageDataFactory.create(logo));
        // 设置居左
        image.setTextAlignment(TextAlignment.LEFT);
        // 图片大小
        image.scaleToFit(150, 200);

        Paragraph imageParagraph = new Paragraph().add(image);
        Paragraph title = new Paragraph("广州慕可生物科技有限公司\n对账单").setFont(font).setTextAlignment(TextAlignment.CENTER);
        Table table = new Table(UnitValue.createPercentArray(3)).useAllAvailableWidth();
        table.addCell(new Cell().add(imageParagraph).setBorder(Border.NO_BORDER));
        table.addCell(new Cell().add(title).setBorder(Border.NO_BORDER));
        document.add(table);
    }

    /**
     * 页脚
     */
    private void addPageFooter(Document document, PdfDocument pdf, PdfPage page) throws IOException {
        PdfFont font = PdfFontFactory.createFont("src/main/resources/AlimamaShuHeiTi-Bold.ttf", PdfEncodings.IDENTITY_H);
        Rectangle pageSize = page.getPageSize();
        Table table = new Table(UnitValue.createPercentArray(1)).useAllAvailableWidth();
        int pageNumber = pdf.getPageNumber(page);
        Paragraph paragraph = new Paragraph(String.valueOf(pageNumber)).setFont(font).setFontSize(12f).setTextAlignment(TextAlignment.CENTER);
        table.addFooterCell(new Cell().add(paragraph).setBorder(Border.NO_BORDER));
        table.setFixedPosition((pageSize.getWidth()-50)/2, 35, 50) ;
        document.add(table);
    }
}
